package com.capgemini.diabeo.activity;

import java.util.ArrayList;
import java.util.List;

import com.capgemini.diabeo.R;
import com.capgemini.diabeo.adapter.DishListAdapter;
import com.capgemini.diabeo.adapter.MenuListAdapter;
import com.capgemini.diabeo.manager.ChangeObserver;
import com.capgemini.diabeo.manager.DishManager;
import com.capgemini.diabeo.manager.SearchManager;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.model.Menu;
import com.capgemini.diabeo.view.MenuItemView;
import com.googlecode.androidannotations.annotations.AfterTextChange;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

@EActivity(R.layout.activity_historique)
public class HistoriqueMenuActivity extends Activity{
	
	public static final String EXTRA_MENU = "extra.menu";
	
	@ViewById
	EditText search;

	@Bean
	MenuListAdapter menuAdapter;

	@ViewById
	ListView menuList;

	@Bean
	DishManager dishManager;
	@Bean
	SearchManager searchManager;
	
	@AfterTextChange(R.id.search)
	void afterTextChangedOnSearch(Editable text) {
	 	// Something Here
		searchAsync(text.toString());
	 }

	
	
	
	@Background
	void searchAsync(String searchString){
		
		updateListContent(searchString);
	}
	
	@UiThread
	void updateListContent(String text){
		
		searchManager.notifyChange(text);
		
		
	}
	

	
	@AfterViews
	public void afterViews() {

		List<Menu> menus = dishManager.retrieveHistoryMenu();
		
//		menuAdapter.setManager(searchManager);
		menuAdapter.setMenuList(menus);
		menuList.setAdapter(menuAdapter);
		
		menuList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long arg3) {
				
				Menu menu = ((MenuItemView)view).getMenu();
				
				Intent intent = new Intent();
				intent.setClass(getBaseContext(), HistoriqueDishActivity_.class);
				intent.putExtra(EXTRA_MENU, menu);
				startActivity(intent);
				
			}
		});
		
		searchManager.registerChangeObserver(new ChangeObserver() {
			
			@Override
			public void onChange(String text) {
				
				MenuListAdapter searchAdapter = new MenuListAdapter();

				if(!text.equals("")){
				List<Menu> menus = menuAdapter.getMenuList();
				List<Menu> searchList = new ArrayList<Menu>();
				
				for (Menu menu : menus) {
					
					if(menu.getMealTime().toString().toLowerCase().contains(text)){
						searchList.add(menu);
					}
						
				}	
				searchAdapter.setMenuList(searchList);
				menuList.setAdapter(searchAdapter);
				}else{
					menuList.setAdapter(menuAdapter);
				}
				
			}
		});
	}
	
	

}
